<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>宿舍数据重置工具</title>
    <style>
        body {
            font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Helvetica Neue', Arial, sans-serif;
            max-width: 600px;
            margin: 0 auto;
            padding: 20px;
            line-height: 1.6;
        }
        h1 {
            color: #333;
            text-align: center;
        }
        .container {
            background-color: #f5f5f5;
            padding: 20px;
            border-radius: 8px;
            box-shadow: 0 2px 4px rgba(0,0,0,0.1);
        }
        button {
            background-color: #4CAF50;
            color: white;
            border: none;
            padding: 12px 24px;
            text-align: center;
            text-decoration: none;
            display: inline-block;
            font-size: 16px;
            margin: 10px 5px;
            cursor: pointer;
            border-radius: 4px;
            transition: background-color 0.3s;
        }
        button:hover {
            background-color: #45a049;
        }
        button.danger {
            background-color: #f44336;
        }
        button.danger:hover {
            background-color: #da190b;
        }
        button.secondary {
            background-color: #008CBA;
        }
        button.secondary:hover {
            background-color: #007B9A;
        }
        .logs {
            background-color: #222;
            color: #0f0;
            padding: 15px;
            border-radius: 4px;
            margin-top: 20px;
            font-family: monospace;
            height: 300px;
            overflow-y: auto;
        }
        .info-box {
            background-color: #e7f3fe;
            border-left: 6px solid #2196F3;
            margin: 10px 0;
            padding: 10px;
        }
    </style>
</head>
<body>
    <h1>宿舍数据重置工具</h1>
    <div class="container">
        <div class="info-box">
            <p>此工具将帮助您清除和重新生成宿舍数据。请按照以下步骤操作：</p>
            <ol>
                <li>先点击「清除宿舍数据」按钮</li>
                <li>然后点击「生成新的宿舍数据」按钮</li>
                <li>最后刷新应用页面查看效果</li>
            </ol>
        </div>
        
        <div style="text-align: center;">
            <button id="clearBtn" class="danger">清除宿舍数据</button>
            <button id="generateBtn" class="secondary">生成新的宿舍数据</button>
            <button id="viewBtn" class="secondary">查看当前数据</button>
        </div>
        
        <div class="logs" id="logs"></div>
    </div>

    <script>
        // 日志输出函数
        function log(message) {
            const logs = document.getElementById('logs');
            const logItem = document.createElement('div');
            logItem.textContent = `[${new Date().toLocaleTimeString()}] ${message}`;
            logs.appendChild(logItem);
            logs.scrollTop = logs.scrollHeight;
        }

        // 检查localStorage环境
        function checkLocalStorage() {
            if (typeof localStorage === 'undefined') {
                log('错误：浏览器不支持localStorage');
                return false;
            }
            return true;
        }

        // 清除宿舍数据
        document.getElementById('clearBtn').addEventListener('click', function() {
            if (!checkLocalStorage()) return;
            
            try {
                localStorage.removeItem('dormitoryStorage');
                localStorage.removeItem('mock_dormitories'); // 同时清除旧的键名
                log('✓ 宿舍数据已成功清除！');
                log('请继续点击「生成新的宿舍数据」按钮');
            } catch (error) {
                log(`✗ 清除数据失败: ${error.message}`);
            }
        });

        // 生成新的宿舍数据
        document.getElementById('generateBtn').addEventListener('click', function() {
            if (!checkLocalStorage()) return;
            
            log('开始生成宿舍数据...');
            
            const dormitories = [];
            let dormitoryId = 1;
            
            // 生成随机入住人数
            const getRandomOccupancy = (capacity) => Math.floor(Math.random() * (capacity + 1));
            // 生成时间戳
            const getRandomDate = () => new Date().toISOString();

            // 生成所有宿舍数据
            // 一号楼男生宿舍
            [1,2,3,4,5].forEach(floor => {
                for (let roomNum = 1; roomNum <= 10; roomNum++) {
                    dormitories.push({
                        id: String(dormitoryId++),
                        building: '一号楼',
                        roomNumber: String(roomNum).padStart(3, '0'),
                        type: 'male',
                        capacity: 4,
                        currentOccupancy: getRandomOccupancy(4),
                        students: [],
                        floor: floor,
                        status: 'active',
                        notes: '男生宿舍',
                        createdAt: getRandomDate(),
                        updatedAt: getRandomDate()
                    });
                }
            });

            // 二号楼女生宿舍
            [1,2,3,4,5].forEach(floor => {
                for (let roomNum = 1; roomNum <= 10; roomNum++) {
                    dormitories.push({
                        id: String(dormitoryId++),
                        building: '二号楼',
                        roomNumber: String(roomNum).padStart(3, '0'),
                        type: 'female',
                        capacity: 4,
                        currentOccupancy: getRandomOccupancy(4),
                        students: [],
                        floor: floor,
                        status: 'active',
                        notes: '女生宿舍',
                        createdAt: getRandomDate(),
                        updatedAt: getRandomDate()
                    });
                }
            });

            // 学三舍男生宿舍
            [1,2,3,4].forEach(floor => {
                for (let roomNum = 1; roomNum <= 8; roomNum++) {
                    dormitories.push({
                        id: String(dormitoryId++),
                        building: '学三舍',
                        roomNumber: String(roomNum).padStart(3, '0'),
                        type: 'male',
                        capacity: 6,
                        currentOccupancy: getRandomOccupancy(6),
                        students: [],
                        floor: floor,
                        status: 'active',
                        notes: '男生宿舍',
                        createdAt: getRandomDate(),
                        updatedAt: getRandomDate()
                    });
                }
            });

            // 学四舍女生宿舍
            [1,2,3,4].forEach(floor => {
                for (let roomNum = 1; roomNum <= 8; roomNum++) {
                    dormitories.push({
                        id: String(dormitoryId++),
                        building: '学四舍',
                        roomNumber: String(roomNum).padStart(3, '0'),
                        type: 'female',
                        capacity: 6,
                        currentOccupancy: getRandomOccupancy(6),
                        students: [],
                        floor: floor,
                        status: 'active',
                        notes: '女生宿舍',
                        createdAt: getRandomDate(),
                        updatedAt: getRandomDate()
                    });
                }
            });

            // 五号楼女生宿舍
            [1,2,3,4].forEach(floor => {
                for (let roomNum = 1; roomNum <= 12; roomNum++) {
                    dormitories.push({
                        id: String(dormitoryId++),
                        building: '五号楼',
                        roomNumber: String(roomNum).padStart(3, '0'),
                        type: 'female',
                        capacity: 4,
                        currentOccupancy: getRandomOccupancy(4),
                        students: [],
                        floor: floor,
                        status: 'active',
                        notes: '女生宿舍',
                        createdAt: getRandomDate(),
                        updatedAt: getRandomDate()
                    });
                }
            });

            // 学五舍男生宿舍
            [1,2,3,5].forEach(floor => {
                for (let roomNum = 1; roomNum <= 10; roomNum++) {
                    dormitories.push({
                        id: String(dormitoryId++),
                        building: '学五舍',
                        roomNumber: String(roomNum).padStart(3, '0'),
                        type: 'male',
                        capacity: 6,
                        currentOccupancy: getRandomOccupancy(6),
                        students: [],
                        floor: floor,
                        status: 'active',
                        notes: '男生宿舍',
                        createdAt: getRandomDate(),
                        updatedAt: getRandomDate()
                    });
                }
            });

            // 留学生公寓
            [1,2,3].forEach(floor => {
                for (let roomNum = 1; roomNum <= 6; roomNum++) {
                    dormitories.push({
                        id: String(dormitoryId++),
                        building: '留学生公寓',
                        roomNumber: String(roomNum).padStart(3, '0'),
                        type: 'mixed',
                        capacity: 2,
                        currentOccupancy: getRandomOccupancy(2),
                        students: [],
                        floor: floor,
                        status: 'active',
                        notes: '混合宿舍',
                        createdAt: getRandomDate(),
                        updatedAt: getRandomDate()
                    });
                }
            });

            // 研究生楼女生部分
            for (let floor = 1; floor <= 2; floor++) {
                for (let roomNum = 1; roomNum <= 8; roomNum++) {
                    dormitories.push({
                        id: String(dormitoryId++),
                        building: '研究生楼',
                        roomNumber: `F${floor}W${String(roomNum).padStart(2, '0')}`,
                        type: 'female',
                        capacity: 2,
                        currentOccupancy: getRandomOccupancy(2),
                        students: [],
                        floor: floor,
                        status: 'active',
                        notes: '女生宿舍',
                        createdAt: getRandomDate(),
                        updatedAt: getRandomDate()
                    });
                }
            }

            // 研究生楼男生部分
            for (let floor = 1; floor <= 2; floor++) {
                for (let roomNum = 1; roomNum <= 8; roomNum++) {
                    dormitories.push({
                        id: String(dormitoryId++),
                        building: '研究生楼',
                        roomNumber: `F${floor}M${String(roomNum).padStart(2, '0')}`,
                        type: 'male',
                        capacity: 2,
                        currentOccupancy: getRandomOccupancy(2),
                        students: [],
                        floor: floor,
                        status: 'active',
                        notes: '男生宿舍',
                        createdAt: getRandomDate(),
                        updatedAt: getRandomDate()
                    });
                }
            }

            // 保存数据
            try {
                localStorage.setItem('dormitoryStorage', JSON.stringify(dormitories));
                log(`✓ 宿舍数据生成完成！共生成 ${dormitories.length} 个宿舍`);
                
                const buildings = [...new Set(dormitories.map(d => d.building))];
                log('生成的楼栋列表：');
                buildings.forEach(building => {
                    const count = dormitories.filter(d => d.building === building).length;
                    log(`- ${building}: ${count} 个宿舍`);
                });
                
                log('✓ 请刷新应用页面查看更新后的宿舍数据');
            } catch (error) {
                log(`✗ 保存数据失败: ${error.message}`);
            }
        });

        // 查看当前数据
        document.getElementById('viewBtn').addEventListener('click', function() {
            if (!checkLocalStorage()) return;
            
            try {
                const data = localStorage.getItem('dormitoryStorage');
                if (data) {
                    const dormitories = JSON.parse(data);
                    log(`当前宿舍数据：共 ${dormitories.length} 个宿舍`);
                    
                    const buildings = [...new Set(dormitories.map(d => d.building))];
                    log('当前楼栋列表：');
                    buildings.forEach(building => {
                        const count = dormitories.filter(d => d.building === building).length;
                        log(`- ${building}: ${count} 个宿舍`);
                    });
                } else {
                    log('未找到宿舍数据！');
                }
            } catch (error) {
                log(`✗ 读取数据失败: ${error.message}`);
            }
        });

        // 初始化页面时检查当前数据状态
        log('工具已加载完毕');
        log('请按照提示操作清除并重新生成宿舍数据');
    </script>
</body>
</html>